﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECC_EXT_HttpLib;
using System.Threading;

namespace ECCSystem
{
    public partial class frm_Ext_GetAccountInfo : Form
    {
        public frm_Ext_GetAccountInfo(String qText)
        {
            InitializeComponent();
            this.webBrowser1.Url = new Uri(Application.StartupPath + "\\Ext\\HTML\\Menu.htm");
            this.pMailQuestion = qText;
            resAccountInfo = null;
        }



        private String pMailQuestion;
        public AccountInfo resAccountInfo { get; private set; }

        private void frm_Ext_GetAccountInfo_Load(object sender, EventArgs e)
        {
            try
            {
                this.webBrowser1.Url = new Uri(Application.StartupPath + "\\Ext\\HTML\\Menu.htm");
            }
            catch (System.UnauthorizedAccessException ex)
            {

                ExtUtility.logger.Debug(ex.Message);
                if (ex.InnerException != null)
                    ExtUtility.logger.Debug(ex.InnerException.Message);
                ExtUtility.logger.Debug(ex.StackTrace);
                throw ex;
            }

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            {
                new Thread(new ThreadStart(GetCheckInfo)).Start();
            }
        }
        private delegate void DG(); 
        private void GetCheckInfo()
        {

        
            //this.Invoke(Delegate.CreateDelegate(this.GetType(),(object) this, "CreateRes"));
            DG dg = new DG(CreateRes);
            this.Invoke(dg);
        }

        private void CreateRes()
        {
            try
            {
                OnlineCheck oc = new OnlineCheck(webBrowser1.Document.DomDocument);
                var res =
                oc.CheckInfo(this.pMailQuestion);

                resAccountInfo = oc.GetProfile();
            }
            catch (System.UnauthorizedAccessException ex)
            {

                ExtUtility.logger.Debug(ex.Message);
                if (ex.InnerException != null)
                    ExtUtility.logger.Debug(ex.InnerException.Message);
                ExtUtility.logger.Debug(ex.StackTrace);
                throw ex;
            } 
            catch(Exception ex)
            {

                ExtUtility.logger.Debug(ex.Message);
                if (ex.InnerException != null)
                    ExtUtility.logger.Debug(ex.InnerException.Message);
                ExtUtility.logger.Debug(ex.StackTrace);
                throw ex;
            }

        }


    }
}
